class Solution():
    def longestCommonPrefix(self, strs):
        if not strs:    #strs为空，返回空
            return ""
        prefix = strs[0]    #假设最长公共前缀为第一个字符串
        for s in strs[1:]:
            while not s.startswith(prefix): #逐步缩小公共字符串
                prefix = prefix[:-1]
        return prefix
    #采用分治法，迭代法
#示例
strs = ['flower','flow','flight']
str2 = ['next','next one','next test']
print(Solution().longestCommonPrefix(strs))
print(Solution().longestCommonPrefix(str2))